﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Peak.Core.DataInterfaces;
using Peak.Core;
using SharpArch.Data.NHibernate;
using NHibernate.LambdaExtensions;
using NHibernate.Criterion;

namespace Peak.Data
{
  public class UserRepository : NHibernateRepositoryWithTypedId<User, int>,IUserRepository
  {
    public User GetByUserName(string userName)
    {
      ICriteria c = Session.CreateCriteria(typeof(User));
      IList list = c.Add(SqlExpression.Like<User>(f => f.UserName,userName)).List();
      if(list.Count == 0)
        return null;
      return list[0] as User;
    }

    public User GetByToken(string token)
    {
      ICriteria c = Session.CreateCriteria(typeof(User));
      c.CreateAlias("Token", "t");
      IList list = c.Add(Expression.Eq("t.TokenId",token)).List();
      if (list.Count == 0)
        return null;
      return list[0] as User;
    }
    
    public User GetByEmail(string email)
    {
      ICriteria c = Session.CreateCriteria(typeof(User));
      IList list = c.Add<User>(f => f.Email == email).List();
      if (list.Count == 0)
        return null;
      return list[0] as User;
    }
  }
}
